//
//  HFeedBackViewController.m
//  ChangXing
//
//  Created by Zhen Huang on 2018/5/17.
//  Copyright © 2018年 Zhen Huang. All rights reserved.
//

//用户反馈
#import "HFeedBackViewController.h"
@interface HFeedBackViewController ()
<UITextViewDelegate>
@property (nonatomic, strong) UITextView *textview;
@end

@implementation HFeedBackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kBackGroundColor;
    [self.view addSubview:self.textview];
    UIButton *SubmitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    SubmitBtn.frame = CGRectMake(28,ScreenHeight - 150,ScreenWidth - 56,55);
    if (iPhone5) {
        SubmitBtn.height = 40;
        SubmitBtn.layer.cornerRadius = 20;
    }
    [SubmitBtn setTitle:@"提交" forState:UIControlStateNormal];
    [SubmitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    SubmitBtn.backgroundColor = kNavBlueColor;
    SubmitBtn.layer.masksToBounds = YES;
    SubmitBtn.layer.cornerRadius = 25;
    
    [[SubmitBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(id x) {
        NSString *Url = F(@"%@/users/feedback", BaseUrl);
        NSDictionary *content = @{@"content":self.textview.text?:@""};
        NSString *param = [self dictionaryToJson:content];
        NSString *Token = F(@"Token %@", [[TMCache sharedCache] objectForKey:@"Token"]);
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            [self receivePOSTWithTokenURLString:Url method:@"POST" body:param Token:Token Blocks:^(NSDictionary *tempData, NSInteger state) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [HCommon popupSuccessMessage:@"反馈成功"];
                    [self.navigationController popViewControllerAnimated:YES];
                });
            }];
        });
    }];
    [self.view addSubview:SubmitBtn];
}
- (UITextView *)textview {
    if (!_textview) {
        _textview = [[UITextView alloc]initWithFrame:CGRectMake(20, NavBarHeight + 10, ScreenWidth - 40, 200)];
        _textview.delegate = self;
        _textview.layer.borderWidth = 1;
        _textview.layer.borderColor = kWidgetBackGroundColor.CGColor;
        _textview.layer.cornerRadius = 5;
        _textview.backgroundColor = kWidgetBackGroundColor;
        _textview.layer.masksToBounds = YES;
        _textview.textColor = [Networking colorWithHexString:@"#C3E5FF"];
        [_textview setFont:[UIFont fontWithName:@"Arial" size:16]];
    }
    return _textview;
}
//点击界面的任意地方，回收键盘
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}
//限制输入字数为60个字
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if (range.length == 1 && text.length == 0) {
        return YES;
    }
    else if (textView.text.length >= 60) {
        textView.text = [textView.text substringToIndex:60];
        return NO;
    }
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
